package com.example.denglu;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class InterestSelectActivity extends AppCompatActivity {

    private CheckBox cbReading, cbPhotography, cbPingPong;
    private Button btnBack, btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_select);

        // 初始化控件
        initViews();

        // 设置按钮点击事件
        setButtonListeners();
    }

    // 初始化控件
    private void initViews() {
        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingPong = findViewById(R.id.cb_pingpong);
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);
    }

    // 设置按钮点击事件
    private void setButtonListeners() {
        // 返回按钮
        btnBack.setOnClickListener(v -> finish());
        btnBack.setOnTouchListener(getButtonTouchListener());

        // 确定按钮
        btnConfirm.setOnClickListener(v -> confirmSelection());
        btnConfirm.setOnTouchListener(getButtonTouchListener());
    }

    // 按钮触摸反馈监听器
    private View.OnTouchListener getButtonTouchListener() {
        return (v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setBackgroundColor(getResources().getColor(R.color.button_pressed));
            } else if (event.getAction() == MotionEvent.ACTION_UP
                    || event.getAction() == MotionEvent.ACTION_CANCEL) {
                v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            }
            return false;
        };
    }

    // 处理选择结果
    private void confirmSelection() {
        List<String> selectedInterests = new ArrayList<>();

        if (cbReading.isChecked()) selectedInterests.add("阅读");
        if (cbPhotography.isChecked()) selectedInterests.add("摄影");
        if (cbPingPong.isChecked()) selectedInterests.add("乒乓球");

        // 将选择的兴趣返回
        Intent intent = new Intent();
        intent.putExtra("interests", String.join("、", selectedInterests));
        setResult(RESULT_OK, intent);
        finish();
    }
}
